import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_app/r.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/empty_view.dart';
import 'package:flutter_app/widget/gallery/gallery.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../photo_gallery_logic.dart';
import '../photo_gallery_state.dart';

/**
 * @Description: 相册 可视化采集资料 list
 * @Author: 下雨了
 * @CreateDate: 2022-7-12 10:47:15
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-7-12 10:47:18
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
class PhotoGalleryListPage extends StatefulWidget {
  final String title;
  const PhotoGalleryListPage({Key? key, required this.title}) : super(key: key);

  @override
  _PhotoGalleryListPageState createState() => _PhotoGalleryListPageState();
}

class _PhotoGalleryListPageState extends State<PhotoGalleryListPage> with AutomaticKeepAliveClientMixin {
  final logic = Get.find<PhotoGalleryLogic>();
  final state = Get.find<PhotoGalleryLogic>().state;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return GetBuilder<PhotoGalleryLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: Colors.white,
        body: (state.entity?.length ?? 0) > 0
            ? ListView.builder(
              padding: EdgeInsets.only(top: 0.r),
              itemCount: state.entity?.length,
              itemBuilder: (BuildContext context, int index) {
                return ListItem(state.entity![index]);
              },
            ) : EmptyView(),
      );
    });
  }

  Widget ListItem(PhotoGalleryInfo item) {
    return Container(
      margin: EdgeInsets.only(bottom: 14.5.r),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Visibility(
            visible: item.list?.length != 0,
            child: Text(item.title ?? "", style: TextStyle(color: MyColors.FF666666, fontSize: 14.sp),),
          ),
          SizedBox(height: 10.r,),
          GridView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: item.list?.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                mainAxisSpacing: 2.r,
                crossAxisSpacing: 2.r,
                childAspectRatio: 1),
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                child: Stack(
                  alignment: Alignment.topRight,
                  children: [
                    Container(
                      width: 113.r,
                      height: 113.r,
                      child: Image.file(
                        File(item.list![index].thumbnail ?? ""),
                        fit: BoxFit.cover,
                        width: 113.r,
                      ),
                    ),
                    Visibility(
                      visible: item.list![index].type == "video",
                      child: Center(
                        child: Image.asset(R.assetsImgIcon57, width: 30.r,),
                      ),
                    ),
                    Visibility(
                      visible: item.list![index].type == "video",
                      child: Align(
                        alignment: Alignment(0.9, 0.95),
                        child: Text(item.list![index].duration ?? "", style: TextStyle(color: MyColors.FFFFFFFF, fontSize: 12.sp), ),
                      ),
                    ),
                    Obx(() {
                      return Visibility(
                        visible: state.isEdit.value,
                        child: InkWell(
                          child: Container(
                            padding: EdgeInsets.only(top: 5.r, right: 5.r, left: 10.r, bottom: 10.r),
                            child: item.list![index].isChose.value
                              ? Image.asset(R.assetsImgIcon81, width: 18.r,)
                              : Image.asset(R.assetsImgIcon80, width: 18.r,),
                          ),
                          onTap: () {
                            item.list![index].isChose.value = !item.list![index].isChose.value;
                          },
                        ),
                      );
                    }),
                  ],
                ),
                onTap: () {
                  List<GalleryModel> list = [];
                  item.list?.forEach((element) {
                    list.add(GalleryModel(
                      id: element.path ?? "",
                      url: element.path ?? "",
                      time: element.dateTime,
                      address: element.address,
                    ));
                  });
                  gallery(list, showIndex: index);
                },
              );
            },
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;
}
